package onlinebanking.server.objects;

import java.sql.ResultSet;
import java.sql.SQLException;

import onlinebanking.server.SimpleServer;
import onlinebanking.sql.SQLConnection;
import onlinebanking.sql.SQLTable;

public class AuthenticationCode extends QueryObject{
	
	protected int tacid;
	protected int personalaccountid;
	protected int xval;
	protected int offset;
	protected int bankaccountid;
	
	public int getTacid() {
		return tacid;
	}

	public int getPersonalaccountid() {
		return personalaccountid;
	}

	public int getXval() {
		return xval;
	}

	public int getOffset() {
		return offset;
	}

	public int getBankaccountid() {
		return bankaccountid;
	}

	public AuthenticationCode(SimpleServer server){
		super();
	}
	
	public AuthenticationCode(int tacid){
		super();
		String sqlstatement = "SELECT tacid,personalaccountid,xval,offset,bankaccountid FROM tacs WHERE tacid = ? ;";
		//user inherited variables
		this.sqltable.setSQLStatement(sqlstatement);
		this.sqltable.setIntParam(1, tacid);
		this.resultset = this.sqltable.executeSQL();
		_loadinfo();
	}
	
	private void _loadinfo(){
		try {
			//if no results, return
			if(!resultset.first())	return;	
			
			//loads variables from db
			tacid = resultset.getInt("tacid");
			personalaccountid = resultset.getInt("personalaccountid");
			xval = resultset.getInt("xval");
			offset = resultset.getInt("offset");
			bankaccountid = resultset.getInt("bankaccountid");
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static int getTACid(int personalaccountid, int bankaccountid){
		int TACid = -1;
		
		String sqlstatement = "SELECT tacid FROM tacs JOIN accountowners USING (accountownerid) WHERE personalaccountid = ? AND bankaccountid = ?;";
		
		SQLTable sqltable = new SQLTable(SQLConnection.getInstance().getConnection());
		sqltable.setSQLStatement(sqlstatement);
		sqltable.setIntParam(1, personalaccountid);
		sqltable.setIntParam(2, bankaccountid);
		ResultSet resultset = sqltable.executeSQL();
		
		try {
			if(resultset.next()){
				TACid = resultset.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return TACid;
	}
	
	public static int getConfirmationCount(int transactionid){
		int count = -1;		
		
		String sqlstatement = "SELECT COUNT(*) FROM transconfirmations WHERE transactionid = ?";
			
		SQLTable sqltable = new SQLTable(SQLConnection.getInstance().getConnection());
		sqltable.setSQLStatement(sqlstatement);
		sqltable.setIntParam(1, transactionid);
		
		ResultSet resultset = sqltable.executeSQL();

		try {
			if(resultset.next()){
				count = resultset.getInt(1) ;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return count;
	}
	
	public static ResultSet getConfirmations(int transactionid){
		String sqlstatement = "SELECT transconfirmationid, encryptedpac, xval, offset " +
				"FROM transconfirmations JOIN tacs USING (tacid) WHERE transactionid = ?;";
		
		SQLTable sqltable = new SQLTable(SQLConnection.getInstance().getConnection());
		sqltable.setSQLStatement(sqlstatement);
		sqltable.setIntParam(1, transactionid);
		
		ResultSet resultset = sqltable.executeSQL();
		
		return resultset;
	}
	
	public static boolean AuthenticateTAC(int bankaccountid,long TAC){
		String sqlstatement = "SELECT tac FROM bankaccounts WHERE bankaccountid = ?;";
		
		SQLTable sqltable = new SQLTable(SQLConnection.getInstance().getConnection());
		sqltable.setSQLStatement(sqlstatement);
		sqltable.setIntParam(1, bankaccountid);
		
		ResultSet resultset = sqltable.executeSQL();

		try {
			if(resultset.next()){
				long dbtac = resultset.getLong(1) ;
				return (dbtac == TAC);				
			}
			return false;
			
		} catch (SQLException e) {
			
			e.printStackTrace();
			return false;
		}
		
		
	}

}
